const mongoose=require('mongoose');
const config=require('../config');
const skus=require('../clean/skus');
const Book=require('../models/book');
const Comment=require('../models/comment');

mongoose.connect(config.db);

0&&(async function(){

    Book.find({},(err,books)=>{
        if(err)return console.error('Find books ERR:\n',err);
        let bookSkus=books.map(book=>book.sku);

        (function removeIIFE(){
            let sku=bookSkus.pop();
            if(!sku)return console.log('Books removed');

            if(skus.books.some(_sku=>_sku == sku))return removeIIFE();//存在，则不删除此条数据

            Book.remove({sku:sku},(err)=>{
                if(err)return console.error('Remove book ERR:\n',sku,err);
                removeIIFE();
            });

        })();

    });

})();

(async function(){

    Comment.find({},(err,comments)=>{
        if(err)return console.error('Find comment ERR:\n',err),process.exit();
        let commentSkus=comments.map(comment=>comment.sku);

        (function removeIIFE(){
            let sku=commentSkus.pop();
            if(!sku)return console.log('Comment removed'),process.exit();

            if(skus.comments.some(_sku=>_sku == sku))return removeIIFE();//存在，则不删除此条数据

            Comment.remove({sku:sku},(err)=>{
                if(err)return console.error('Remove comment ERR:\n',sku,err),process.exit();
                removeIIFE();
            });

        })();

    });

})();
